{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57a1c774",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "            close_price  close_shift_1  close_shift_2\n",
      "Date                                                 \n",
      "2014-01-02         7.99            NaN            NaN\n",
      "2014-01-03         7.84           7.99            NaN\n",
      "2014-01-06         7.48           7.84           7.99\n",
      "2014-01-07         7.43           7.48           7.84\n",
      "2014-01-08         7.42           7.43           7.48\n",
      "            close_price  close_shift_1  close_shift_2  simple_return_1  \\\n",
      "Date                                                                     \n",
      "2014-01-02         7.99            NaN            NaN              NaN   \n",
      "2014-01-03         7.84           7.99            NaN        -0.018773   \n",
      "2014-01-06         7.48           7.84           7.99        -0.045918   \n",
      "2014-01-07         7.43           7.48           7.84        -0.006684   \n",
      "2014-01-08         7.42           7.43           7.48        -0.001346   \n",
      "\n",
      "            simple_return_2  \n",
      "Date                         \n",
      "2014-01-02              NaN  \n",
      "2014-01-03              NaN  \n",
      "2014-01-06        -0.063830  \n",
      "2014-01-07        -0.052296  \n",
      "2014-01-08        -0.008021  \n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 读取股票数据数据\n",
    "stock = pd.read_csv('dataset/stockszA.csv', index_col='Trddt')\n",
    "# 筛选万科股票数据\n",
    "vanke = stock[stock['Stkcd']==2]\n",
    "# print(vanke.head())\n",
    "\n",
    "# 股票的收盘价\n",
    "close_price = vanke['Clsprc']\n",
    "# 处理时间序列\n",
    "close_price.index = pd.to_datetime(close_price.index)\n",
    "close_price.index.name = 'Date'\n",
    "# print(close_price.head())\n",
    "\n",
    "# 将收盘价滞后\n",
    "merge_close = pd.DataFrame({'close_price':close_price,\\\n",
    "    'close_shift_1':close_price.shift(1),'close_shift_2':close_price.shift(2)})\n",
    "print(merge_close.head())\n",
    "\n",
    "# 计算单期简单收益率\n",
    "simple_return_1 = (merge_close['close_price'] - merge_close['close_shift_1']) / merge_close['close_shift_1'] \n",
    "simple_return_1.name='simple_return_1'\n",
    "# 计算两期简单收益率\n",
    "simple_return_2 = (merge_close['close_price'] - merge_close['close_shift_2']) / merge_close['close_shift_2'] \n",
    "simple_return_2.name='simple_return_2'\n",
    "\n",
    "# 合并数据\n",
    "calc_data = pd.merge(merge_close, pd.DataFrame(simple_return_1), left_index=True, right_index=True)\n",
    "calc_data['simple_return_2'] = simple_return_2\n",
    "print(calc_data.head())\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b7591a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
